package com.ews.onlineexchange.validator;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.inject.Inject;
import javax.inject.Named;
import com.ews.onlineexchange.service.UserService;

@Named
public class UsernameValidator implements Validator {
	
	@Inject
	UserService userService;

	@Override
	public void validate(FacesContext facesContext,
		      UIComponent component, Object value) throws ValidatorException {
		if (userService.existUsername(value.toString())) {
			FacesMessage msg =
	            new FacesMessage("山东政务服务门户网站登录名已被占用","无效的山东政务服务门户网站登录名");
			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
	        throw new ValidatorException(msg);
		}
	}

}
